﻿<!DOCTYPE>

<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=EDGE;IE=11;IE=8,chrome=1" />
    <title>无限产能排产查看</title>
    <link rel="stylesheet" type="text/css" href="/InSiteWebApplication/Content/AnyGantt/css/anychart-ui.min.css" />        
    <link rel="stylesheet" type="text/css" href="/InSiteWebApplication/Content/AnyGantt/fonts/css/anychart-font.min.css" />
   
   
    <link href="/InSiteWebApplication/Content/Easyui/themes/metro/easyui.css" rel="stylesheet" />
    <link href="/InSiteWebApplication/Content/Easyui/custom-easyui.css" rel="stylesheet" />
    <link href="/InSiteWebApplication/Content/Easyui/themes/icon.css" rel="stylesheet" />

    <style type="text/css">
        .anychart-credits
        {
            display:none;
            }
    </style>
</head>
<body>
    <div class="panelSection">    
        <div class="easyui-panel " title="操作区域" style="padding:3px;width: 100%" 
        data-options="closable:false,collapsible:true,minimizable:false,maximizable:false"> 
             <a href="#" id="btnSaveData" class="easyui-linkbutton" data-options="" style="margin-top: 0px;">
                            保存调整</a>
        </div>
    </div> 

    <div id="container"></div>

</body>
</html>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/ReferenceJsLib.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.easyui.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/easyui-lang-zh_CN.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/CustomCommon.js" type="text/javascript"></script>
<script type="text/javascript" src="/InSiteWebApplication/Content/AnyGantt/zh-cn.js"></script>  
<script type="text/javascript" src="/InSiteWebApplication/Content/AnyGantt/anychart-base.min.js"></script>        
<script type="text/javascript" src="/InSiteWebApplication/Content/AnyGantt/anychart-ui.min.js"></script>        
<script type="text/javascript" src="/InSiteWebApplication/Content/AnyGantt/anychart-exports.min.js"></script>        
<script type="text/javascript" src="/InSiteWebApplication/Content/AnyGantt/anychart-gantt.min.js"></script>        
<script type="text/javascript" src="/InSiteWebApplication/Content/AnyGantt/anychart-data-adapter.min.js"></script>

<script type="text/javascript">
    var page = {
        parentID:'',//父窗体div
        rootPath: getRootPath(), //网站根路径
        ajaxUrl: getRootPath() + "/Ashx/ApsHandler/ApsInfiniteCapacity.ashx",
        param: [{apsPlanInfoID:'a89485f8-52b4-40e8-96f0-8f703a718a53'},{apsPlanInfoID:'f611f4b9-c58f-4d54-9708-a0dd1593e6fa'}], //需要查看的id
        //初始化页面
        init: function () {
            page.bind();
            page.loadInfiniteCapacity();
        },
        bind:function(){
             $('#btnSaveData').on("click", function () {
                page.saveAdjustedResult();
             });
        },
        treeData:null,
        returnData:null,
        recountApsData:function(parentId,itemId,differ){
            page.returnData.forEach(item=>{
                if(item.id==parentId)//批次级计划
                {
                    let start=dayjs(item.periods[0].start).add(differ,'ms').format("YYYY/MM/DD HH:mm:ss");
                    let end=dayjs(item.periods[0].end).add(differ,'ms').format("YYYY/MM/DD HH:mm:ss");

                    item.periods[0].start=start;
                    item.periods[0].end=end;
                    item.customData.differ=differ;
                    item.customData.constarttime=start;
                    item.customData.conendtime=end;
                    //更新treeData
                    var treeItem= page.treeData.filter(_treeItem=>{
                        return _treeItem.get('id')==item.id;
                    });
                    if(treeItem!=null){
                    
                       var prePeriods=treeItem[0].get('periods');
                       prePeriods[0].start=item.periods[0].start;
                       prePeriods[0].end=item.periods[0].end;
                       treeItem[0].set('periods',prePeriods);
                       treeItem[0].get('customData').differ=differ;

                       //console.log(treeItem);
                    }
                    

                }else if(item.parent==parentId)//工序级计划
                {
                    item.periods[0].start=dayjs(item.periods[0].start).add(differ,'ms').format("YYYY/MM/DD HH:mm:ss");
                    item.periods[0].end=dayjs(item.periods[0].end).add(differ,'ms').format("YYYY/MM/DD HH:mm:ss");

                    //更新treeData
                    var treeItem= page.treeData.filter(_treeItem=>{
                        return _treeItem.get('id')==item.id;
                    });
                    if(treeItem!=null){

                       var prePeriods=treeItem[0].get('periods');
                       prePeriods[0].start=item.periods[0].start;
                       prePeriods[0].end=item.periods[0].end;
                       treeItem[0].set('periods',prePeriods);

                    }
                }
            });

        },
        drawingFunction:function () {
  // get the shapes of the element
  var shapes = this["shapes"];
  // get the shape to be modified
  var path = shapes["path"];
  // get the bounds of the element
  var bounds = this["predictedBounds"];
    
//this["period"];//当前渲染的块
var item=this["item"];
if(item.numChildren()>0){
   // console.log(this.period); 
    //path.fill('green');   
}


 // console.log(this["predictedBounds"]);

  var h = bounds.height;
  var t = bounds.top;
  var l = bounds.left;
  var r = l + bounds.width;
  var h1 = t + h;   
  
let period=this['period'];
let customData=period.customData;
if(customData!=null){

//console.log(customData);

if(customData.isHolidate!=null){

h1+=4;
t-=3;
path.fill('#ccc');   
//console.log(this['period']);

}

}
   
  //var h4 = h/4;
  //var h2 = h/2;

  // draw a rounded rectangle
//  path.moveTo(l + h4, h1 - h4)
//  path.arcTo(h4,h4, -270, 180)
//  path.lineTo(r - h4, t + h4)
//  path.arcTo(h4, h4, -90, 180)
//  path.lineTo(l + h2, h1 - h4)
//  path.close(); 

  path.moveTo(l, t);
  path.lineTo(l , h1 );
  path.lineTo(r, h1);
  path.lineTo(r, t);

  //console.log(path.domElement());

  path.close(); 


},
        //保存调整后的结果
        saveAdjustedResult:function(){
            var apsCons=[];
            var adjustData=page.returnData.forEach(function(item){
               if( item.parent==null&&item.customData.differ!=0){
                    apsCons.push({
                        apsPlanInfoID:item.id,
                        containerID:item.customData.containerid,
                        apsType:item.customData.apsType,
                        workflowid:item.customData.workflowid,
                        STARTTIME:item.customData.constarttime,
                        ENDTIME:item.customData.conendtime
                    })
               }
            });

            if(apsCons.length==0){
                 displayMessage("未调整计划", false);
                return;
            }

            //console.log(page.returnData);
            var param = {
                action: "adjustInfiniteAps",
                conAps: JSON.stringify(apsCons)
            };
            $.ajax({
                type: "Post",
                url: page.ajaxUrl,
                data: param,
                cashe: false,
                async: true,
                beforeSend: function () {
                      $.messager.progress({ text: '', msg: '处理中...' });
                },
                success: function (data) {
                     $.messager.progress('close');
                    var result = JSON.parse(data);
                    if (result.Result) { 
                         page.resetContainer();   
                         page.loadInfiniteCapacity();                   
                         displayMessage("调整成功", true);
                    } else {
                        displayMessage(result.Message, false);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                     $.messager.progress('close');
                    alert(XMLHttpRequest.responseText);
                }
            });
        },
        resetContainer:function(){
            $("#container").empty();
        },
        loadInfiniteCapacity: function () { //无限产能加载方法
            anychart.onDocumentReady(function () {

                anychart.data.loadJsonFile(page.ajaxUrl + "?action=getStepInfiniteAps&param="+JSON.stringify(page.param), function (result) {

                    anychart.format.outputLocale('zh-cn');
                    //anychart.format.outputDateTimeFormat('yyyy-MM-dd');
                    anychart.format.inputDateTimeFormat("yyyy/MM/dd HH:mm:ss");

                    if(result.Result==false){
                        displayMessage(result.Message, false);
                        return;
                    }

                    var data=JSON.parse(result.ReturnData);
                    page.returnData=JSON.parse(result.ReturnData);
                    
                    //console.log(data);

                    // create data tree on raw data
                    var treeData = anychart.data.tree(data, 'as-table');

                    page.treeData=treeData;
                    // create resource gantt chart
                    chart = anychart.ganttResource();

                    chart.splitterPosition(400); //左边grid宽度

                    // set data for the chart
                    chart.data(treeData);

                    //设置列
                    var dataGrid = chart.dataGrid();
                    dataGrid.column(0)
                            .title('')
                            .width(30)
                            .cellTextSettings({ hAlign: 'center' });

                    dataGrid.column(1)
                            .title("任务")
                            .cellTextSettings({ hAlign: 'left', width: 180 });

                    dataGrid.column(2)
                            .title("开始时间")
                            .cellTextSettings({ hAlign: 'left', width: 100 })
                            .format(item=>{
                                let customData=item.get("customData");
                                if(customData!=null)
                                    return customData.constarttime;
                            });
                    dataGrid.column(3)
                            .title("结束时间")
                            .cellTextSettings({ hAlign: 'left', width: 100 })
                            .format(item=>{
                                 let customData=item.get("customData");
                                 if(customData!=null)
                                    return customData.conendtime;
                            });
                     //datagrid tooltips
                    dataGrid.tooltip().useHtml(true); 

                    var periods = chart.getTimeline().periods();

                    // set the height of periods
                    //periods.height(40);
                    // draw custom periods
                    periods.rendering().drawer(page.drawingFunction);

                    //图形块的label
                    var periodLabels =periods.labels();
                    periodLabels.enabled(true);
                    periodLabels.useHtml(true);
                    periodLabels.fontColor("#104d89");
                    periodLabels.fontWeight(600);
                    periodLabels.format(function() {
                        let customData=this.period.customData;
                        if(customData!=null){
                            if(customData.isHolidate!=null&&customData.isHolidate=='1'){
                                var text='<span style="color:#fff;font-wieght:bold">'+this.period.text+'</span>';
                                return text;     
                            }
                        }
                    });

                    // configure tooltips of the timeline
                chart.getTimeline().tooltip().useHtml(true);
                chart.getTimeline().tooltip().format(function() {

                      var period=this.period;
                      var numChildren = this.item.numChildren();

                      var startDate = anychart.format.dateTime(this.start, "yyyy-MM-dd HH:mm:ss");
                      var endDate = anychart.format.dateTime(this.end, "yyyy-MM-dd HH:mm:ss");


                      var parentText ="<span style='font-weight:600;font-size:12pt'>" + 
                                     '开始:'+startDate + "<br>"+
                                     '结束:'+endDate+"</span>"

                      var childText ="<span style='font-weight:600;font-size:12pt'>" + 
                                     '开始:'+startDate + "<br>"+
                                     '结束:'+endDate+"</span>"

                     
                      // identify the resource type and display the corresponding text
                      if (numChildren > 0) {
                        //console.log(this.item.getChildAt(0).get('periods'));
                        return parentText;
                      } else {
                        //console.log(this.item.get('periods'));
                        return childText;
                      }

                });

                    // make chart editable
                    //chart.editing(true);
                    chart.getTimeline().edit(true);

                    chart.contextMenu(false);//禁用右键

                    // set container id for the chart
                    chart.container('container');

                    // initiate chart drawing
                    chart.draw();
                                       
                    // zoom chart all dates range
                    chart.fitAll();

                    // Enable chart redrawing.
                    chart.autoRedraw(true);
                                       

                    //事件绑定
                    treeData.listen('treeItemUpdate', function (e) {

                        var val = e['value'];
                        var date=new Date(val);//移动后的日期

                        date=dayjs(anychart.format.dateTime(date,"yyyy-MM-dd HH:mm:ss")).set('second', 0);

                        var item=e['item'];
                        var parentItem=item.getParent();
                        if(parentItem==null)
                            return;
                        var allChildItems=parentItem.getChildren();

                        if (e['path'][2]=='end'){
                        
                        }else if (e['path'][2]=='start'){                            

                            let preItem=page.returnData.find(i=>{return i.id==item.get('id')}) ;//调整之前的值
                            let preDate=dayjs(preItem.periods[e['path'][1]].start);
                            let differ=dayjs(date).diff(preDate);

//                            console.log('现在：'+dayjs(date).format("YYYY-MM-DD HH:mm:ss"));
//                            console.log('现在：'+dayjs(date).valueOf());
//                            console.log('之前：'+preDate.format("YYYY-MM-DD HH:mm:ss"));
//                            console.log('之前：'+preDate.valueOf());
//                            console.log('此次差异：'+differ);
//                            console.log('计算后的结果'+preDate.add(differ,'ms').format("YYYY-MM-DD HH:mm:ss"));

                            //console.log(allChildItems);
                           page.recountApsData(parentItem.get('id'),item.get('id'),differ);
                           


                        }

                    });
                });
            });
        }
    }

$(function () {
    //获取传参
    page.parentID = getParam("parentDiv");
    
    page.param=parent.$("#" + page.parentID).dialog('options').popupData;
    page.init();

});
</script>
